// packageUser/pages/hold-list/index.js
import {getPositions} from "../../../service/User"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hold: {
            list: [],
            loading: false,
            reset: false,
            error: false,
            total: 0
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({"hold.loading": true})
    },
    
    /*
    * 
    * 获取我的持仓列表
    * */
    onLoadList: async function (e) {
        try {
            let paging = e.detail.paging;
            let list = this.data.hold.list;
            let isReset = this.data.hold.reset;
            let res = await getPositions({
                "page": paging.page,
                "per-page": paging.pageSize
            });
            let newList = res.itemList.map(function (item) {
                return {
                    c_fund_id: item.c_fund_id,
                    c_fund_name: item.c_fund_name,
                    t_date: item.t_date,
                    n_nav: item.n_nav,
                    n_refmark_value: item.n_refmark_value,
                    n_hold_shares: item.n_hold_shares,
                    yingkui: item.yingkui,
                    fenhong_shouyi: item.fenhong_shouyi,
                    c_cust_type: item.c_cust_type
                }
            })
            list = isReset ? newList : list.concat(newList);
            this.setData({
                "hold.list": list,
                "hold.reset": false,
                "hold.loading": false,
                "hold.total": res.totalCount
            });
        } catch (e) {
            this.setData({
                "hold.error": true
            })
        } finally {
            wx.stopPullDownRefresh();
        }
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({"hold.reset": true})
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({"hold.loading": true})
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})